package com.demo.gateway.util;

import java.io.IOException;
import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;

import com.demo.gateway.pojo.RecordDO;

/**
 * @author: demo
 * @date: 2020年7月2日
 * @Document :
 */
public class GenerateSqlUtil {
	public static void generateSqlMapper(Class<?> obj, String tableName) throws IOException {
		Field[] fields = obj.getDeclaredFields();
		String param = null;
		String cameCaseColumn = null;
		String underScoreCaseColumn = null;
		StringBuilder sql = new StringBuilder();
		if (tableName == null || tableName.equals("")) {
			// 未传表明默认用类名
			tableName = obj.getName().substring(obj.getName().lastIndexOf(".") + 1);
		}
		/**
		 * 以下部分生成建表Sql
		 */
		sql.append("drop table if exists ").append(tableName).append(";\r\n");
		sql.append("create table ").append(tableName).append("( \r\n");
		for (Field f : fields) {
			cameCaseColumn = f.getName();
			underScoreCaseColumn = cameCaseColumn;
			for (int i = 0; i < cameCaseColumn.length(); i++) {
				if (Character.isUpperCase(cameCaseColumn.charAt(i))) {
					// 将javabean中小驼峰命名变量的“大写字母”转换为“_小写字母”
					underScoreCaseColumn = cameCaseColumn.substring(0, i) + '_'
							+ cameCaseColumn.substring(i, i + 1).toLowerCase()
							+ cameCaseColumn.substring(i + 1, cameCaseColumn.length());
				}

			}

			sql.append(underScoreCaseColumn).append(" ");
			param = f.getType().getTypeName();
			if ("java.lang.Integer".equals(param)) {
				sql.append("INTEGER");
			} else {
				// 根据需要自行修改
				sql.append("VARCHAR(255)");
			}
			sql.append(",\n");
		}
		sql.delete(sql.lastIndexOf(","), sql.length())
				.append("\n)ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;\r\n");
		System.out.println("建表语句==>");
		System.out.println(sql);

		StringBuilder insert = new StringBuilder();
		StringBuilder insertMapper = new StringBuilder();
		StringBuilder update = new StringBuilder();

		insert.append("insert into ").append(tableName).append(" (");
		update.append("update ").append(tableName).append(" set ");
		for (Field f : fields) {
			cameCaseColumn = f.getName();
			underScoreCaseColumn = cameCaseColumn;
			for (int i = 0; i < cameCaseColumn.length(); i++) {
				if (Character.isUpperCase(cameCaseColumn.charAt(i))) {
					// 将javabean中小驼峰命名变量的“大写字母”转换为“_小写字母”
					underScoreCaseColumn = cameCaseColumn.substring(0, i) + '_'
							+ cameCaseColumn.substring(i, i + 1).toLowerCase()
							+ cameCaseColumn.substring(i + 1, cameCaseColumn.length());
				}
			}
			
			insert.append(underScoreCaseColumn).append(",");
			insertMapper.append(String.format("#{%s},", cameCaseColumn));
			update.append(underScoreCaseColumn).append(String.format("=#{%s},", cameCaseColumn));
		}
		String lastInsert = StringUtils.substringBeforeLast(insert.toString(), ",").concat(") values (")
				.concat(StringUtils.substringBeforeLast(insertMapper.toString(), ",")).concat(")");
		String lastUpdate = StringUtils.substringBeforeLast(update.toString(), ",");
		System.out.println("插入语句==>");
		System.out.println(lastInsert);
		System.out.println("更新语句==>");
		System.out.println(lastUpdate);

	}

	public static void main(String[] args) throws IOException {
		generateSqlMapper(RecordDO.class, "gateway_record");
	}
}